package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.Ad.pagerAd;

public class MainActivity2 extends AppCompatActivity implements View.OnFocusChangeListener, View.OnClickListener, DatePickerDialog.OnDateSetListener {

    private SharedPreferences X;
    public static EditText from;
    public static EditText to;
    public static String dataString;
    private Button data;
    private Button search;
    private Boolean isdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ViewPager viewPager = findViewById(R.id.main2_viewpager);
        pagerAd pagerAd = new pagerAd(MainActivity2.this);
        viewPager.setAdapter(pagerAd);
        from = findViewById(R.id.from);
        to = findViewById(R.id.to);
        from.setOnFocusChangeListener(this);
        to.setOnFocusChangeListener(this);
        data = findViewById(R.id.data);
        search = findViewById(R.id.search);
        search.setOnClickListener(this);
        data.setOnClickListener(this);
        isdata = false;
        X = getSharedPreferences("fromto", 0);
        String from2 = X.getString("from", null);
        if (from2 != null){
            from.setText(from2);
        }
        String to2 = X.getString("to", null);
        if (to2 != null){
            to.setText(to2);
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
                SharedPreferences.Editor edit = X.edit();
                edit.putString("from", String.valueOf(from.getText()));
                edit.putString("to",String.valueOf(to.getText()));
                edit.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.data:
                DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity2.this,this,2024,6,23);
                datePickerDialog.show();
                break;
            case R.id.search:
                if (from.getText().toString().trim().length() == 0 || to.getText().toString().trim().length() == 0 || isdata == false ){
                    Toast.makeText(MainActivity2.this,"请补充完整信息",Toast.LENGTH_SHORT).show();
                }else {
                    isdata = false;
                    Toast.makeText(MainActivity2.this,isdata.toString(),Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(MainActivity2.this,MainActivity3.class));

                }
                break;
        }
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        isdata = true;
        dataString = year+"年"+month+"月"+dayOfMonth+"日";
        data.setText(dataString);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(from.getWindowToken(),0);
        return super.onTouchEvent(event);
    }
}